package com.cykj.pos.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cykj.common.config.RuoYiConfig;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.common.utils.StringUtils;
import com.cykj.pos.controller.TestDTO.HttpClientHandler;
import com.cykj.pos.domain.BizMerchTransactions;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.domain.BizMicroInfo;
import com.cykj.pos.domain.dto.*;
import com.cykj.pos.domain.vo.BizMerchParentRelated;
import com.cykj.pos.enums.bizstatus.BizStatusContantEnum;
import com.cykj.pos.profit.dto.*;
import com.cykj.pos.profit.dto.MerchantDTO;
import com.cykj.pos.profit.dto.MerchantDict;
import com.cykj.pos.service.*;
import com.cykj.pos.service.impl.BizMerchantReceiverServiceImpl;
import com.cykj.pos.util.LoginUserUtils;
import com.cykj.pos.util.PosOneUtil;
import com.cykj.system.service.ISysUserService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pos/api/v2/merchant")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "我的伙伴")
public class PosV2AppMerchantController {

    private final IBizMerchTransactionsService merchTransactionsService;

    private final IBizMerchantService merchantService;

    private final IBizVerifyCodeService verifyCodeService;

    private final ISysUserService sysUserService;

    private final IBizMicroInfoService microInfoService;

    private final RuoYiConfig ruoYiConfig;

    private final IBizTerminalRateListService bizTerminalRateListService;

    private final IBizTransactionAmount bizTransactionAmount;

    private final BizMerchantReceiverServiceImpl bizMerchantReceiverService;

    @ApiOperation(value = "获取商户及子商户列表")
    @ApiResponses({@ApiResponse(code = 200, response = BizMerchant.class, message = "业务数据响应成功")})
    @PostMapping("/list")
    public AjaxResult queryTerminalList(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        List<BizMerchant> merchantList = merchantService.getAllSubNodeAndIncludeSelf(merchant.getMerchId());
        ajaxResult.put("data", merchantList);
        return ajaxResult;
    }

    @ApiOperation(value = "获取当前商户信息")
    @ApiResponses({@ApiResponse(code = 200, response = BizMicroInfo.class, message = "业务数据响应成功")})
    @PostMapping("/current")
    public AjaxResult getCurrentMerchat(@RequestBody BizMerchant merchantBrief) {
        AjaxResult ajaxResult = AjaxResult.success();
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);

        LambdaQueryWrapper<BizMicroInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(BizMicroInfo::getMerchId, merchant.getMerchId());
        BizMicroInfo microInfo = microInfoService.getOne(lqw);
        // 获取头像信息
        SysUser sysUser = sysUserService.selectUserById(userId);
        String portrait = sysUser.getPortrait();
        Map<String, Object> merchanInfo = new HashMap<>();
        merchanInfo.put("merchantBrief", merchant);
        merchanInfo.put("merchantDetail", microInfo);
        merchanInfo.put("portrait", portrait);
        ajaxResult.put("data", merchanInfo);
        return ajaxResult;
    }

    @ApiOperation(value = "获取直接子商户字典")
    @ApiResponses({@ApiResponse(code = 200, response = MerchantDict.class, message = "业务数据响应成功")})
    @PostMapping("/direct/child/dict")
    public AjaxResult getChildMerchantDictList(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        Long userId = LoginUserUtils.getLoginUserId();
        // List<MerchantDict> dicts = merchantService.getChildMerchartDictList(userId);
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        Long merchId = merchant.getMerchId();
        // 设置商户编号
        merchantDTO.setMerchId(merchId);
        if (merchantDTO.getNickName() == "") {
            List<MerchantDict> dicts = merchantService.selectPagedPartnerList(merchantDTO);
            ajaxResult.put("data", dicts);
        } else {
            List<MerchantDict> dictAll = merchantService.selectPagedPartnerListAll(merchantDTO);
            ajaxResult.put("data", dictAll);
        }
        return ajaxResult;
    }

    @ApiOperation(value = "获取我的伙伴列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "nickName", value = "用户姓名", dataType = "string", required = false, paramType = "body"),
            @ApiImplicitParam(name = "verifyStatus", value = "商户报件状态，0-报件审核中，1-报件成功，2-异常", dataType = "string", required = false, paramType = "body"),
            @ApiImplicitParam(name = "monthType", value = "数据月份，'this'-本月，'last'-上月", dataType = "string", required = false, paramType = "body"),
            @ApiImplicitParam(name = "pageNo", value = "当前页号，默认-1", dataType = "int", required = true, paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "当前页大小，默认-1", dataType = "int", required = true, paramType = "body")})
    @ApiResponses({@ApiResponse(code = 200, response = PartnerDTO.class, message = "业务数据响应成功")})
    @PostMapping("/mypartner/list")
    public AjaxResult getPartnerList(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        merchantDTO.setUserId(LoginUserUtils.getLoginUserId());
        ajaxResult.put("data", merchantService.getPagedPartnerList(merchantDTO));
        ajaxResult.put("partnerCounts", merchantService.getChildMerchantCounts(merchantDTO.getUserId(), merchantDTO.getVerifyStatus(), "", merchantDTO.getNickName()));
        return ajaxResult;
    }

    @ApiOperation(value = "获取我的伙伴详情")
    @ApiResponses({@ApiResponse(code = 200, response = PartnerDTO.class, message = "业务数据响应成功")})
    @PostMapping("/mypartner/detail")
    public AjaxResult getPartnerDetail(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        // ajaxResult.put("data",merchantService.getPartnerDetail(LoginUserUtils.getLoginUserId()));
        ajaxResult.put("data", merchantService.getPartnerDetail(merchantDTO.getUserId()));
        return ajaxResult;
    }

    @ApiOperation(value = "获取我的伙伴详情交易额统计图")
    @PostMapping("/mypartner/amount/statistical")
    public AjaxResult getPartnerAmountStatistical(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();

        List<PartnerDetailDTO> partnerDetailDTOList = merchantService.getPartnerAmountStatistical(merchantDTO);
        ajaxResult.put("data", partnerDetailDTOList);
        return ajaxResult;
    }

    @ApiOperation(value = "获取我的伙伴详情激活累计统计图")
    @PostMapping("/mypartner/machines/statistical")
    public AjaxResult getPartnerMachinesStatistical(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        List<PartnerDetailDTO> partnerDetailDTOList = merchantService.getPartnerMachinesStatistical(merchantDTO);
        ajaxResult.put("data", partnerDetailDTOList);
        return ajaxResult;
    }

    @ApiOperation(value = "商户入驻")
    @ApiResponses({@ApiResponse(code = 200, response = MicroMerchantDTO.class, message = "商户入驻成功")})
    @PostMapping("/register")
    public AjaxResult merchantRegister(@RequestBody MicroMerchantDTO microMerchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success("商户入驻成功");
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        String mobile = sysUser.getPhonenumber();
        String verifyCode = microMerchantDTO.getVerifyCode();
/*        BizStatusContantEnum bizStatus = verifyCodeService.verifyCodeValidate(mobile, verifyCode);
        if (bizStatus != BizStatusContantEnum.SMS_SUCCESS) {
            return AjaxResult.error(bizStatus.getName());
        }*/
        microMerchantDTO.setUserId(sysUser.getUserId());
        merchantService.microMerchantRegister(microMerchantDTO);
        return ajaxResult;
    }

    @ApiOperation(value = "登录密码设置")
    @ApiImplicitParams({@ApiImplicitParam(name = "password", value = "登录密码密文", dataType = "string", required = true, paramType = "body")})
    @ApiResponses({@ApiResponse(code = 200, response = PartnerInviteDTO.class, message = "登录密码重置成功")})
    @PostMapping("/password/reset")
    public AjaxResult resetLoginPassword(@RequestBody PartnerInviteDTO partnerInviteDTO) {
        AjaxResult ajaxResult = AjaxResult.success("登录密码重置成功");
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        String mobile = sysUser.getPhonenumber();
        String verifyCode = partnerInviteDTO.getVerifyCode();
        BizStatusContantEnum bizStatus = verifyCodeService.verifyCodeValidate(mobile, verifyCode);
        if (bizStatus != BizStatusContantEnum.SMS_SUCCESS) {
            return AjaxResult.error(bizStatus.getName());
        }
        //密码密文处理，前端算法解密-新算法加密-保存
        String password = SecurityUtils.encryptPassword(partnerInviteDTO.getPassword());
        sysUserService.resetUserPwd(sysUser.getUserName(), password);
        return ajaxResult;
    }

    @ApiOperation(value = "获得伙伴邀请码")
    @PostMapping("/mypartner/invite")
    public AjaxResult getPartnerInviteCode(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        // 获取用户
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        // 获取用户id
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        // SysUser sysUser1 = sysUserService.selectUserById(userId);
        Map<String, Object> merchanInfo = new HashMap<>();
        merchanInfo.put("merchCode", merchant.getMerchCode());
        String invitationUrl = ruoYiConfig.getInvitationUrl();
        merchanInfo.put("url", invitationUrl);
        ajaxResult.put("data", merchanInfo);
        return ajaxResult;
    }

    @ApiOperation(value = "验证设置支付密码")
    @PostMapping("/payment/validate")
    public AjaxResult paymentValidate(@RequestBody PaymentPassUpdateDTO merchantDTO) {
        // 验证码验证
        String verifyCode = merchantDTO.getVerifyCode();
        String mobile = merchantDTO.getMobile();
        BizStatusContantEnum bizStatus = verifyCodeService.verifyCodeValidate(mobile, verifyCode);
        if (bizStatus != BizStatusContantEnum.SMS_SUCCESS) {
            return AjaxResult.error(bizStatus.getName());
        }
        AjaxResult ajaxResult = AjaxResult.success();
        // 获取用户
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        // 获取用户id
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        Long merId = merchant.getMerchId();
        BizMicroInfo microInfo = microInfoService.getBizMicroInfoByMerchId(merId);
        String merchIdCard = microInfo.getMerchIdcard(); // 数据库中的身份证号
        String cardNo = merchantDTO.getCardNo();
        if (merchIdCard == null || merchIdCard.length() <= 0) {
            return AjaxResult.error(BizStatusContantEnum.CARD_NO_IS_NULL.getName());
        }
        /*String dbCardNo = merchIdCard.substring(merchIdCard.length()-6);// 截取后6位
        if(!cardNo.equals(dbCardNo)){
            return AjaxResult.error(BizStatusContantEnum.PAYMENT_CARDNO_ERROR.getName());
        }*/
        return AjaxResult.success(BizStatusContantEnum.PAYMENT_VALIDATE_SUCCESS.getName());
    }

    @ApiOperation(value = "验证重置修改支付密码")
    @PostMapping("/payment/validatePassUpdate")
    // 验证码   原支付密码
    public AjaxResult paymentValidatePassUpdate(@RequestBody PaymentPassUpdateDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        // 获取用户
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        // 获得用户id
        Long userId = sysUser.getUserId();
        // 获取用户信息
        SysUser loginUser = sysUserService.selectUserById(userId);
        // 获取支付
        String dbPaymentPassword = loginUser.getPaymentPassword();
        // 获得接口传递过来的输入支付密码
        String password = merchantDTO.getPassword();
        // 验证
        boolean flag = SecurityUtils.matchesPassword(password, dbPaymentPassword);
        if (!flag) {
            // 原支付密码输入有误 PAYMENT_ORIGINAL_PASSWORD_ERROR
            return AjaxResult.error(BizStatusContantEnum.PAYMENT_ORIGINAL_PASSWORD_ERROR.getName());
        }
        return ajaxResult;
    }


    @ApiOperation(value = "修改支付密码")
    @PostMapping("/payment/updatePassword")
    public AjaxResult updatePaymentPass(@RequestBody PaymentPassUpdateDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        // 获取用户
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        String paymentPassword = SecurityUtils.encryptPassword(merchantDTO.getPassword());
        sysUser.setPaymentPassword(paymentPassword);
        sysUserService.resetPaymentPass(sysUser);
        return ajaxResult;
    }

    // 修改头像

    @ApiOperation(value = "修改头像")
    @PostMapping("/updatePortrait")
    public AjaxResult updatPortrait(@RequestBody UpdatePortraitDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        // 获取用户
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        String portraitUrl = merchantDTO.getPortraitUrl();
        sysUser.setPortrait(portraitUrl);
        sysUserService.resetPortraitUrl(sysUser);
        return ajaxResult;
    }

    // 验证身份证是否唯一

    @ApiOperation(value = "验证身份证是否唯一")
    @PostMapping("/idCordIsExist")
    public AjaxResult idCordIsExist(@RequestBody MicroMerchantDTO dto) { //merchIdcard
        AjaxResult ajaxResult = AjaxResult.success("验证身份证是否唯一成功");
        // 获取用户
        boolean b = microInfoService.idCordIsExist(dto.getMerchIdcard());
        ajaxResult.put("data", b);
        return ajaxResult;
    }


    @ApiOperation(value = "查询商户")
    @PostMapping("/queryMerchantInfo")
    public AjaxResult queryMerchantInfo(@RequestBody MerchantDTO merchantDTO) {
        AjaxResult ajaxResult = AjaxResult.success("查询商户信息成功");
        /*SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long userId = sysUser.getUserId();
        merchantDTO.setUserId(userId);*/
        // 查询所有信息
        List<MerchantInfoDTO> list = merchantService.queryMerchantInfo(merchantDTO);
        ajaxResult.put("num", list.size());
        // 按规则查询
        List<MerchantInfoDTO> orderlyList = merchantService.queryOrderlyMerchantInfo(merchantDTO);
        ajaxResult.put("data", orderlyList);
        return ajaxResult;
    }

    @ApiOperation(value = "商户的设备信息")
    @PostMapping("/queryEquipmentInfo")
    public AjaxResult queryMerchantEquipmentInfo(@RequestBody MerchantInfoDTO merchantInfoDTO) {
        BizAgentidAppkeyDTO bizAgentidAppkeyDTO= bizMerchantReceiverService.getAgentidAppkey(SecurityUtils.getLoginUser().getUser().getUserId());
        AjaxResult ajaxResult = AjaxResult.success("查询商户设备信息成功");
        Map<String, String> map = new HashMap<>();
        // 通过sn_code去查找子商户信息
        MerchantInfoDTO data = merchantService.queryMerchantEquipmentInfo(merchantInfoDTO.getId());
        if (StringUtils.isEmpty(data.getTerminalCode())) {
            try {
                map.put("sn", merchantInfoDTO.getId());
                map.put("key", bizAgentidAppkeyDTO.getKey());
                map.put("mersource", "OUTMER");
                map.put("agentid", bizAgentidAppkeyDTO.getAgentid());
                map.put("sign", PosOneUtil.generateSignature(map));
                //SN信息查询
                String result = snInfo(map);
                System.out.printf("sn信息查询返回data==========" + snInfo(map));
                JSONObject jsonObject = JSONObject.parseObject(result);
                String retCode = jsonObject.getString("ret_code");
                if (retCode.equals("0")) {
                    JSONObject dataList = jsonObject.getJSONObject("data");
                    String terminalNo = dataList.getString("terminal_no");
                    //更新终端编码
                    merchantService.updateTerminalNo(merchantInfoDTO.getId(), terminalNo);
                    data.setTerminalCode(terminalNo);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ajaxResult.put("data", data);
        return ajaxResult;
    }

    @ApiOperation(value = "查询商户交易信息")
    @PostMapping("/queryMerchantDealInfo")
    public AjaxResult queryMerchantDealInfo(@RequestBody MerchantInfoDTO merchantInfoDTO) {
        AjaxResult ajaxResult = AjaxResult.success("查询商户交易信息成功");
        // 先查询交易笔数
        List<BizMerchTransactions> list = merchTransactionsService.queryDealNum(merchantInfoDTO.getId());
        // 查询本月交易金额
        BigDecimal thisBigDecimal = merchTransactionsService.queryNowMonthDeal(merchantInfoDTO);
        BigDecimal bigDecimal = merchTransactionsService.queryAllMonthDeal(merchantInfoDTO);
        // 查询本月交易金额 刷卡 扫码 闪付 分开
        TransactionsDTO data = merchTransactionsService.queryNowMonthDealWay(merchantInfoDTO);
        data.setNum(list.size());
        data.setAddAmount(bigDecimal);
        data.setThisMonthAmount(thisBigDecimal);
        ajaxResult.put("data", data);
        return ajaxResult;
    }

    @ApiOperation(value = "查询商户交易信息-总业绩")
    @PostMapping("/getAllTransInfo")
    public AjaxResult queryAllTransactionInfo(@RequestBody TransPerformanceDTO transPerformanceDTO) {
        AjaxResult ajaxResult = AjaxResult.success("查询总业绩交易信息成功");
        // 查询总业绩
        List<TransPerformanceDTO> list = merchTransactionsService.queryAllTransactionInfo(transPerformanceDTO);
        ajaxResult.put("data", list);
        return ajaxResult;
    }

    @ApiOperation(value = "查询商户交易信息-个人业绩")
    @PostMapping("/getMerchantTransInfo")
    public AjaxResult queryMerchantTransactionInfo(@RequestBody TransPerformanceDTO transPerformanceDTO) {
        AjaxResult ajaxResult = AjaxResult.success("查询商户交易信息成功");
        // 查询个人业绩
        List<TransPerformanceDTO> list = merchTransactionsService.queryMerchantTransactionInfo(transPerformanceDTO);
        ajaxResult.put("data", list);
        return ajaxResult;
    }

    @ApiOperation(value = "查询商户交易信息-团队业绩")
    @PostMapping("/getTeamTransInfo")
    public AjaxResult getTeamTransInfo(@RequestBody TransPerformanceDTO transPerformanceDTO) {
        AjaxResult ajaxResult = AjaxResult.success("查询团队交易信息成功");
        // 查询团队业绩
        List<TransPerformanceDTO> list = merchTransactionsService.queryTeamTransactionInfo(transPerformanceDTO);
        ajaxResult.put("data", list);
        return ajaxResult;
    }

    @ApiOperation("修改我的伙伴费率")
    @PostMapping("/modifyRate")
    @Transactional
    public AjaxResult modifyRate(@RequestBody ModifyRateDTO modifyRateDTO) {
        merchantService.modifyRate(modifyRateDTO);
        return AjaxResult.success("修改成功");
    }

    @ApiOperation("回显我的伙伴费率")
    @PostMapping("/echoRate")
    @Transactional
    public AjaxResult echoRate(@RequestBody ModifyRateDTO modifyRateDTO) {
        AjaxResult ajaxResult = AjaxResult.success();
        ModifyRateDTO modifyRateDTO1 =  merchantService.echoRate(modifyRateDTO);
        ajaxResult.put("data",modifyRateDTO1);
        return ajaxResult;
    }

    @ApiOperation("SN信息查询")
    public String snInfo(Map<String, String> data) {
        String url = "http://wx.wenwenpay.com.cn:6080/vipf/merchantinfo.do?action=handleAction&beanName=terminalinfoDao&Methods=load";
        return HttpClientHandler.noPictureRequest(url, data);
    }
    @ApiOperation("添加商户merchId代理merchId")
    @PostMapping("/addMerchId")
    @Transactional
    public AjaxResult addMerchId() {
        AjaxResult ajaxResult = AjaxResult.success();
        //查询商户的pos
        List<String> pos = bizTransactionAmount.selectAllPos();
        //查询商户的上级代理
        for (String posCode:pos){
            BizMerchParentRelated bizMerchParentRelated =  bizTransactionAmount.selectParenId(posCode);
            if (bizMerchParentRelated != null){
                bizMerchParentRelated.setPosCode(posCode);
                //加入到pos1.0通知里
                bizTransactionAmount.updateBizTransactionAmountMerchIdAndParentId(bizMerchParentRelated);
            }else {
                System.out.println("这台pos机没有商户了"+posCode);
            }

        }
        return ajaxResult;
    }
}
